package com.mayi.servlet.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mayi.dao.IUserDao;
import com.mayi.dao.impl.UserDaoImpl;
import com.mayi.pojo.User;
import com.mayi.servlet.BaseServlet;
import com.mayi.utils.JsonUtils;
import com.mayi.vo.Result;
import sun.net.httpserver.HttpsServerImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.mayi.utils.JsonUtils.getRequestPayload;

/**
 * @Description: userServlet
 * @Auther: yacong_liu
 * @Date: 2019/7/2
 * @version: 1.0
 */
@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    private IUserDao userDao = new UserDaoImpl();

    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        // 调用业务层处理请求
        List<User> users = this.userDao.findAll();
        request.setAttribute("users", users);

        // 控制界面跳转
        request.getRequestDispatcher("/WEB-INF/view/user/list.jsp").forward(request, response);

    }

    public void listAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JsonUtils.SUCCESS(response, this.userDao.findAll());
    }


    public void save(HttpServletRequest request, HttpServletResponse response) throws Exception {

        User user = JsonUtils.requestPayloadtoJavaObject(request, User.class);

        int i = -1;
        if (user.getId() != null) {
            i = this.userDao.update(user);
        } else {
            i = this.userDao.save(user);
        }

        if (i == 1) {
            JsonUtils.SUCCESS(response);
        } else {
            JsonUtils.ERROR(response);
        }

    }

    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");

        if (id != null && !"".equals(id.trim())) {
            User user = this.userDao.findById(Integer.valueOf(id));

            request.setAttribute("user", user);
        }

        request.getRequestDispatcher("/WEB-INF/view/user/" +
                "edit.jsp").forward(request, response);

    }


    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String data = getRequestPayload(request);
        int i = this.userDao.delete(Integer.valueOf(data));

        if (i == 0) {
            Result success = Result.success();

            PrintWriter pw = null;
            try {
                pw = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }

            pw.write(JSON.toJSONString(success));
            pw.flush();

        }

        //response.sendRedirect("/user/list");

    }


}
